package com.example.singleactivitytemplate.ui.base

import android.content.Context
import android.content.pm.ActivityInfo
import android.os.Bundle
import android.view.KeyEvent
import android.view.MotionEvent
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.viewbinding.ViewBinding
import com.blankj.utilcode.util.*


/**
 * Activity 基础类
 * @author Joymed
 */
abstract class BaseActivity<VB : ViewBinding, VM : BaseViewModel> : AppCompatActivity() {
    protected var viewBinding: VB? = null
    protected var mViewModel: VM? = null



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewBinding = getViewBindingClass()
        setContentView(viewBinding?.root)
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE//竖屏

        mViewModel = ViewModelProvider(this)[getViewModelClass()]



        LogUtils.d("onCreate:${javaClass.name}")

        initOnListener()
        initObserver()
        initData()
    }

    override fun attachBaseContext(newBase: Context?) {
        super.attachBaseContext(LanguageUtils.attachBaseContext(newBase))
    }

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        if (BarUtils.isNavBarVisible(this)) {
            BarUtils.setNavBarVisibility(this, false)
        }
    }

    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {

        return super.dispatchTouchEvent(ev)
    }


    /**
     * 显示信息提示弹窗
     * @param msg 需要提示的信息ID
     */
    protected fun showInfo(msg: Int) {
        showInfo(getString(msg))
    }

    /**
     * 显示信息提示弹窗
     * @param msg 需要提示的信息
     */
    protected fun showInfo(msg: String) {

    }

    /**
     * 显示信息提示弹窗
     * @param msg 需要提示的信息
     * @param id 信息类型
     */
    protected fun showInfo(id: Int, msg: String) {
        try {

        } catch (e: Exception) {
            LogUtils.e("ShowInfo:${javaClass.name} - ${e.message}")
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        try {
            LogUtils.d("onDestroy:${javaClass.name}")
            destroy()
        } catch (e: Exception) {
            LogUtils.e("onDestroy:${javaClass.name} - ${e.message}")
        }
    }

    override fun onKeyUp(keyCode: Int, event: KeyEvent): Boolean {
        return false
    }

    protected abstract fun getViewBindingClass(): VB

    protected abstract fun getViewModelClass(): Class<VM>

    protected abstract fun initData()

    protected abstract fun initOnListener()

    protected abstract fun initObserver()

    protected abstract fun destroy()
}